package com.itheima.web;

import com.itheima.R;
import com.itheima.pojo.UmsMenu;
import com.itheima.service.UmsMenuService;
import com.itheima.vo.PageResult;
import com.itheima.vo.UmsMenuNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 袁鹏
 * @date 2022-10-13-12:43
 */
@RestController
@RequestMapping("/menu")
public class UmsMenuController {

    @Autowired
    private UmsMenuService umsMenuService;


    /**
     * 添加后台菜单
     */
    @PostMapping("/create")
    public R create(@RequestBody UmsMenu umsMenu){
        umsMenuService.create(umsMenu);
        return R.success(null);
    }


    /**
     * 根据ID删除后台菜单
     */
    @PostMapping("/delete/{id}")
    public R delete(@PathVariable("id") Long id) {
        umsMenuService.delete(id);
        return R.success(null);
    }


    /**
     * 分页查询后台菜单
     */
    @GetMapping("/list/{parentId}")
    public R pageByParentId(@PathVariable("parentId")Long parentId, Integer pageNum, Integer pageSize){
        PageResult pageInfo = umsMenuService.pageByParentId(parentId, pageNum, pageSize);
        return R.success(pageInfo);
    }


    /**
     * 树形结构返回所有菜单列表
     */
    @GetMapping("/treeList")
    public R treeList(){
        List<UmsMenuNode> tree = umsMenuService.treeList();
        return R.success(tree);
    }

    /**
     * 根据id查询菜单详情
     */
    @GetMapping("{id}")
    public R findById(@PathVariable Integer id){
        return umsMenuService.findById(id);
    }

    /**
     * 修改菜单
     */
    @PostMapping("update/{id}")
    public R updateById(@PathVariable Integer id,@RequestBody UmsMenu umsMenu){
        return umsMenuService.updateById(id,umsMenu);
    }

    /**
     * 修改菜单显示状态
     */
    @PostMapping("updateHidden/{id}")
    public R updateHidden(@PathVariable Integer id,Integer hidden){
        return umsMenuService.updateHidden(id,hidden);
    }

}
